import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    host: 'http://localhost:3000',
    currentUser: null,
    isPlay: false,
    isPlayInfo: false,
    currentSongIndex: 0,
    currentSong: {
      name: '暂未播放音乐',
      singer: '暂无歌手'
    },
    currentSongList: []
  },
  mutations: {
    changeCurrentUser(state, obj) {
      state.currentUser = obj;
    },
    togglePlay(state, boolean) {
      state.isPlay = boolean;
    },
    togglePlayInfo(state, boolean) {
      state.isPlayInfo = boolean;
    },
    initCurrentSongIndex(state, num) {
      state.currentSongIndex = num;
    },
    changeCurrentSongIndex(state, num) {
      if (num == 1) {
        state.currentSongIndex++;
        if (state.currentSongIndex > state.currentSongList.length - 1) {
          state.currentSongIndex--;
        }
      } else {
        state.currentSongIndex--;
        if (state.currentSongIndex < 0) {
          state.currentSongIndex = 0;
        }
      }
    },
    changeCurrentSong(state, obj) {
      let isValid = obj.cover.indexOf(state.host);
      if (isValid != -1) {
        obj.cover = obj.cover.split(':3000')[1];
        obj.content = obj.content.split(':3000')[1];
      }

      if (obj.cover.indexOf('http') != -1) {
        state.currentSong = obj;
      } else {
        obj.cover = state.host + obj.cover;
        obj.content = state.host + obj.content;
        state.currentSong = obj;
      }
    },
    changeCurrentSongList(state, list) {
      state.currentSongList = list;
    }
  },
  actions: {
  },
  modules: {
  }
})
